<?php
/**
 * Created by PhpStorm.
 * User: ruma
 * Date: 2019/2/2
 * Time: 10:07
 */

namespace app\admin\controller;


use app\common\controller\AdminBaseController;
use app\Excepiton\ParameterException;

class Link extends AdminBaseController
{

    protected $db;

    public function _initialize() {
        parent::_initialize();
        $this->db = new \app\admin\model\Link();
    }

    //友链首页
    public function index() {
        $field = $this->db->getAll();
        $this->assign('field', $field);
        return $this->fetch();
    }

    //添加友链
    public function store() {
        $link_id = input('param.link_id');
        if (IS_POST){
            try{
                //传递id即编辑
                if ($link_id){
                    $this->db->editLink();
                    $this->success('链接修改成功,','index');
                }
                //反之添加
                else{
                    $this->db->addLink();
                    $this->success('链接添加成功','index');
                }
            }
            catch (ParameterException $e){
                $str = arrayToString($e->msg);
                $this->error('操作失败'.$str,'index');
            }
        }
        if ($link_id){
            $linkData = $this->db->find($link_id);
        }else{
            $linkData = ['link_name'=>'','link_url'=>'','link_sort'=>100];
        }
        $this->assign('linkData',$linkData);
        return $this->fetch();
    }

    /**
     * 友链删除
     */
    public function del() {
        $link_id = input('post.link_id');
        $this->db->delLink($link_id);
        $this->success('删除成功','index');
    }
}